//hash表的实现
#include <iostream>
#include <string>
#include <vector> 

namespace zlu_algorithm {
	template<class key_type, class value_type>
	struct Node {
		Node(key_type k, value_type v) : key(k), val(v), next(nullptr) {}
		key_type key;
		value_type val;
		Node* next;
	};
	template<class key_t, class value_t>
	class hash_table {
	public:
		using Node = Node<key_t, value_t>;
		hash_table() {
			h.resize(50);
			len = 50;
			std::vector<Node*> t(len, nullptr);
			h.swap(t);
		}
		void insert(key_t key, value_t value) {
			int k = std::hash<key_t>()(key) % len;
			Node* node = new Node(key, value);
			Node* nex = h[k];
			node->next = nex;
			h[k] = node;
			size1++;
			std::cout << "插入元素成功" << std::endl;
		}
		void erase(key_t key) {
			int k = std::hash<key_t>()(key) % len;
			if (h[k] == nullptr) {
				std::cout << "没有这个key" << std::endl;
				return;
			}
			Node* pre = nullptr;
			Node* curr = h[k];
			while (curr && curr->key != key) {
				pre = curr;
				curr = curr->next;
			}
			if (curr == nullptr) {
				std::cout << "没有这个key" << std::endl;
				return;
			}
			std::cout << "删除成功" << std::endl;
			size1--;
			if (pre == nullptr) {
				h[k] = curr->next;
				delete curr;
				return;a
			}
			pre->next = curr->next;
			delete curr;
		}
		value_t operator[](key_t key) {
			int k = std::hash<key_t>()(key) % len;
			if (h[k] == nullptr) {
				std::cout << "没有这个key" << std::endl;
				return value_t();
			}
			Node* pre = nullptr;
			Node* curr = h[k];
			while (curr && curr->key != key) {
				pre = curr;
				curr = curr->next;
			}
			if (curr == nullptr) {
				std::cout << "没有这个key" << std::endl;
				return value_t();
			}
			std::cout << "查找成功" << std::endl;
			return curr->val;
		}
		int size() {
			return size1;
		}
		//begin, end
	private:
		std::vector<Node*> h;
		int len;
		int size1;
	};
};

int main() {

	zlu_algorithm::hash_table<std::string, int> map;
	map.insert("zhou", 12);
	map.insert("liu", 22);
	std::cout << map["zhou"] << std::endl;
	map.erase("zhou");
	std::cout << map["zhou"] << std::endl;

	return 0;
}